package vip.websky.project.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.websky.project.common.error.CommonsRuntimeException;
import vip.websky.project.common.error.ErrorCode;
import vip.websky.project.common.error.ErrorMessage;
import vip.websky.project.common.utils.DateUtils;
import vip.websky.project.common.utils.ObjectConvertUtils;
import vip.websky.project.core.log.KingLog;
import vip.websky.project.module.bo.user.SysRoleBO;
import vip.websky.project.module.dto.RequestDTO;
import vip.websky.project.module.dto.ResponseDTO;
import vip.websky.project.module.dto.request.user.AddRoleReqDTO;
import vip.websky.project.module.dto.request.user.EditRoleReqDTO;
import vip.websky.project.module.dto.request.user.GetRoleReqDTO;
import vip.websky.project.module.dto.response.user.SysRoleRespDTO;
import vip.websky.project.user.error.UserErrorCode;
import vip.websky.project.user.error.UserErrorMessage;
import vip.websky.project.user.server.RoleServer;
import vip.websky.project.user.service.RoleService;

import java.util.List;

/**
 * 系统角色 接口实现类
 *
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/9/25-2:08
 **/
@Slf4j
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired private RoleServer roleServer;

    @KingLog
    @Override
    public ResponseDTO addRole(RequestDTO requestDTO) {
        ResponseDTO<SysRoleRespDTO> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            AddRoleReqDTO addSysRole = JSON.parseObject(requestDTO.getParam(), AddRoleReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            SysRoleBO sysRoleBO = ObjectConvertUtils.copyToDest(addSysRole, SysRoleBO.class);
            //2.处理
            sysRoleBO.setCreateTime(DateUtils.getTimestamp());
            sysRoleBO = roleServer.insertRole(sysRoleBO);
            //3.返回结果
            if (sysRoleBO != null) {
                SysRoleRespDTO respDTO = new SysRoleRespDTO();
                ObjectConvertUtils.convert(sysRoleBO, respDTO);
                responseDTO.setResult(respDTO);
            } else {
                responseDTO.setStatusCode(UserErrorCode.ROLE_ADD_FAIL);
                responseDTO.setStatusMessage(UserErrorMessage.ROLE_ADD_FAIL);
                log.error("角色创建失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @KingLog
    @Override
    public ResponseDTO getRole(RequestDTO requestDTO) {
        ResponseDTO<SysRoleRespDTO> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            GetRoleReqDTO getSysRole = JSON.parseObject(requestDTO.getParam(), GetRoleReqDTO.class);
            //1.参数转换
            SysRoleBO sysRoleBO = ObjectConvertUtils.copyToDest(getSysRole, SysRoleBO.class);
            //2.处理
            sysRoleBO = roleServer.selectRole(sysRoleBO);
            //3.返回结果
            if (sysRoleBO != null) {
                SysRoleRespDTO respDTO = new SysRoleRespDTO();
                ObjectConvertUtils.convert(sysRoleBO, respDTO);
                responseDTO.setResult(respDTO);
            } else {
                responseDTO.setStatusCode(UserErrorCode.ROLE_NOT_EXIST);
                responseDTO.setStatusMessage(UserErrorMessage.ROLE_NOT_EXIST);
                log.error("获取角色信息失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @KingLog
    @Override
    public ResponseDTO getRoleList(RequestDTO requestDTO) {
        ResponseDTO responseDTO = new ResponseDTO<>();
        try {
            //请求参数
            GetRoleReqDTO getSysRoleDTO = JSON.parseObject(requestDTO.getParam(), GetRoleReqDTO.class);
            //TODO 参数校验

            //参数转换
            SysRoleBO sysRoleBO = ObjectConvertUtils.copyToDest(getSysRoleDTO, SysRoleBO.class);
            //处理
            Page<SysRoleBO> sysRoleBOPage = roleServer.selectRoleList(sysRoleBO);
            //返回
            if (sysRoleBOPage.getRecords() != null) {
                //整理转化数据对象
                List<SysRoleRespDTO> sysRoleRespDTOS = ObjectConvertUtils.copyToList(sysRoleBOPage.getRecords(), SysRoleRespDTO.class);
                responseDTO.setResult(sysRoleRespDTOS);//数据
                responseDTO.setTotalCount(String.valueOf(sysRoleBOPage.getTotal()));//总条数
                responseDTO.setTotalPages(String.valueOf(sysRoleBOPage.getPages()));//总页数
            } else {
                responseDTO.setStatusCode(UserErrorCode.ROLE_NOT_EXIST);
                responseDTO.setStatusMessage(UserErrorMessage.ROLE_NOT_EXIST);
                log.error("获取角色列表失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @KingLog
    @Override
    public ResponseDTO editRole(RequestDTO requestDTO) {
        ResponseDTO<SysRoleRespDTO> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            EditRoleReqDTO editRoleDTO = JSON.parseObject(requestDTO.getParam(), EditRoleReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            SysRoleBO sysRoleBO = ObjectConvertUtils.copyToDest(editRoleDTO, SysRoleBO.class);
            //2.处理
            // TODO: 校验参数
            sysRoleBO.setUpdateBy("系统");
            sysRoleBO.setUpdateTime(DateUtils.getTimestamp());
            sysRoleBO = roleServer.updateRole(sysRoleBO);
            //3.返回结果
            if (sysRoleBO != null) {
                SysRoleRespDTO respDTO = new SysRoleRespDTO();
                ObjectConvertUtils.convert(sysRoleBO, respDTO);
                responseDTO.setResult(respDTO);
            } else {
                responseDTO.setStatusCode(UserErrorCode.ROLE_EDIT_FAIL);
                responseDTO.setStatusMessage(UserErrorMessage.ROLE_EDIT_FAIL);
                log.error("修改角色信息失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @KingLog
    @Override
    public ResponseDTO removeRole(RequestDTO requestDTO) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            GetRoleReqDTO getSysRoleDTO = JSON.parseObject(requestDTO.getParam(), GetRoleReqDTO.class);
            //1.参数转换
            SysRoleBO sysRoleBO = ObjectConvertUtils.copyToDest(getSysRoleDTO, SysRoleBO.class);
            //2.处理
            boolean result = roleServer.delRole(sysRoleBO);
            //3.返回结果
            if (result) {
                responseDTO.setResult(result);
            } else {
                responseDTO.setStatusCode(UserErrorCode.ROLE_NOT_EXIST);
                responseDTO.setStatusMessage(UserErrorMessage.ROLE_NOT_EXIST);
                log.error("删除角色信息失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }
}
